package net.lzzy.practices.web.course;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.services.ChapterService;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Api(tags = "章节相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_CHAPTER_ROOT)
public class ChapterApiController {
    private final ChapterService service;

    @Autowired
    public ChapterApiController(ChapterService service) {
        this.service = service;
    }

    @ApiOperation(value = "获取老师课程的所有章节", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.courseId(UUID)<br/>")
    @GetMapping(ApiConstants.ROUTE_CHAPTER_TEACHER)
    public ApiResult getTeachersChapters(@RequestParam String token, @RequestParam(name = "id") UUID courseId){
        List<Chapter> chapters=service.getTeachersChapters(courseId, token);
        return transform(chapters);
    }

    private ApiResult transform(List<Chapter> chapters){
        if (chapters==null){
            return new ApiResult(false, Messages.WRONG_ID+"或"+Messages.NO_PERMISSION,null);
        }
        List<VmChapter> vmChapters=new ArrayList<>();
        chapters.forEach(chapter -> vmChapters.add(new VmChapter(chapter)));
        return new ApiResult(true, vmChapters.size() + "", vmChapters);
    }

    @ApiOperation(value = "获取学生课程的所有章节", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;" +
            "2.courseId(UUID)<br/>")
    @GetMapping(ApiConstants.ROUTE_CHAPTER_STUDENT)
    public ApiResult getStudentsChapter(@RequestParam String token, @RequestParam(name = "id") UUID courseId){
        List<Chapter> chapters=service.getStudentsChapters(courseId, token);
        return transform(chapters);
    }

    private ApiResult addOrUpdate(UUID id, ChapterInfo info){
        String token= info.getToken();
        String name= info.getName();
        String desc= info.getDesc();
        String strCourseId= info.getCourseId();
        boolean open=info.isOpen();
        if (StringUtils.isEmpty(token)
                ||StringUtils.isEmpty(name)
                ||StringUtils.isEmpty(desc)
                ||StringUtils.isEmpty(strCourseId)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        boolean create = id == null;
        UUID courseId=UUID.fromString(strCourseId);
        Chapter chapter=new Chapter();
        chapter.setOpen(open);
        chapter.setName(name);
        chapter.setDescription(desc);
        VmChapter vmChapter;
        if(create){
            vmChapter=new VmChapter(service.add(courseId, chapter, token));
        }else {
            vmChapter=new VmChapter(service.update(id, chapter, token));
        }
        return new ApiResult(true, Messages.SUCCESS.toString(), vmChapter);
    }

    @ApiOperation(value = "老师添加章节到自己的课程", notes = "<b>参数列表：<b/><br>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.courseId(UUID)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;3.chapter(String)<br/>")
    @PostMapping(ApiConstants.ROUTE_CHAPTER_ADD)
    public ApiResult add(@RequestBody ChapterInfo info){
        return addOrUpdate(null, info);
    }

    @ApiOperation(value = "老师更新课程的章节", notes = "<b>参数列表：<b/><br>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.id(UUID)<br/>" +
            "&nbsp;&nbsp;&nbsp;&nbsp;3.chapter(String)<br/>")
    @PostMapping(ApiConstants.ROUTE_CHAPTER_UPDATE)
    public ApiResult update(@PathVariable UUID id, @RequestBody ChapterInfo info) {
        return addOrUpdate(id, info);
    }

    @ApiOperation(value = "老师开放或关闭某一章节", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;" +
            "1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;strChapterId(UUID)<br/>")
    @PostMapping(ApiConstants.ROUTE_CHAPTER_OPEN_CLOSE)
    public ApiResult openCloseChapter(@RequestBody ChapterOpenInfo info){
        String token= info.getToken();
        String strChapterId=info.getChapterId();
        boolean open= info.isOpen();
        if (StringUtils.isEmpty(token)||StringUtils.isEmpty(strChapterId)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        Chapter chapter = service.openCloseChapter(UUID.fromString(strChapterId),open, token);
        if (chapter==null){
            return new ApiResult(false,Messages.WRONG_ID+"或"+Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(), new VmChapter(chapter));
    }

    @ApiOperation(value = "老师删除课程的章节", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;" +
            "1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;strChapterId(UUID)<br/>")
    @DeleteMapping(ApiConstants.ROUTE_CHAPTER_REMOVE)
    public ApiResult delete(@RequestBody ChapterKeyInfo info){
        String token=info.getToken();
        String strChapterId=info.getChapterId();
        if (StringUtils.isEmpty(token)||StringUtils.isEmpty(strChapterId)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        Messages msg=service.remove(UUID.fromString(strChapterId), token);
        return new ApiResult(msg==Messages.SUCCESS, msg.toString(), null);
    }
}
